#include "netimpl.h"
#include "dnsimpl.h"
#include "websocketimpl.h"
#include "httpinstanceimpl.h"

CNetImpl::CNetImpl(void)
{

}
CNetImpl::~CNetImpl(void)
{
	logi("CNetImpl::~CNetImpl");
}
std_method_impl CNetImpl::Init(int Argc, basic_tchar* Argv[], IBase* pRot, IBase* pBase)
{
	rc_assert(pRot != NULL, E_FAIL)
	return pRot->QueryInterface(IID_IComRunningObjectTable, (void**)&m_pRot);
}
std_method_impl CNetImpl::Uninit()
{
    HRESULT		hr = S_OK;

    return hr;
}
std_method_impl CNetImpl::Start(_pinstance hInstance, UINT uType)
{
    HRESULT		hr = S_OK;
 
    InitSocketEnv(2, 2);

	hr = m_pRot->GetObject(CLSID_IAsynFrame, IID_IAsynFrame, (IBase**)&m_pIAsynFrame);
	rc_assert(hr == S_OK, E_FAIL)
	rc_assert(m_pIAsynFrame.m_p != INULL, E_FAIL)

	hr = this->QueryInterface(IID_INet, (void**)&m_pINet);
	rc_assert(hr == S_OK, E_FAIL)
	rc_assert(m_pIAsynFrame.m_p != INULL, E_FAIL)

    return hr;
}
std_method_impl CNetImpl::Stop(UINT uExitCode)
{
    HRESULT		hr = S_OK;

	m_pINet.dispose();
	
    UnInitSocketEnv(2, 2);

    return hr;
}
std_method_impl CNetImpl::CreateProtocol(Net_Protocol netp, IBase** pBase)
{
    rc_assert(m_pIAsynFrame.m_p != INULL, E_FAIL)

    HRESULT hr = S_OK;

    if (netp == Protocol_dns)
    {
        CDnsImpl* pDns = ALLOC_NEW CDnsImpl();
        rc_assert(pDns != NULL, E_FAIL)

 
        hr = pDns->init_class(m_pIAsynFrame, m_pINet);
        rc_assert(hr == S_OK, E_FAIL)

        return pDns->QueryInterface(IID_IDns, (void**)pBase);

    }
    else if (netp == Protocol_websocket)
    {
        CWebSocketImpl* pWebSocket = ALLOC_NEW CWebSocketImpl();
        rc_assert(pWebSocket != NULL, E_FAIL)
    }
    else if (netp == Protocol_http)
	{
		CHttpInstanceImpl* pObject = ALLOC_NEW CHttpInstanceImpl();
		rc_assert(pObject != NULL, E_FAIL)

		hr = pObject->init_class(m_pIAsynFrame, m_pINet);
		rc_assert(hr == S_OK, E_FAIL)

		return pObject->QueryInterface(IID_IHttpInstance, (void**)pBase);
    }
	else {

	}
    return S_OK;
}
